<template>
  <div class="readCard">
    <el-alert style="color: red"
      :title="message"
      type="warning"
      :closable="false">
      <body @beforeunload="unload()" style="background-color: inherit">
      <el-row>
        <OBJECT
          classid="clsid:F1317711-6BDE-4658-ABAA-39E31D3704D3"
          codebase="SDRdCard.cab#version=2,0,1,0"
          width=390
          height=300
          align=center
          hspace=0
          vspace=0
          id=idcard
          name=rdcard
        >
        </OBJECT>
      </el-row>
      <el-row style="margin-top: 10px">

        <el-button @click="myopen_onclick()" type="primary">打开机具</el-button>
<!--        <el-button @click="beginread_onclick()" type="success">开始读卡</el-button>-->
        <!--        <el-button @click="read3_onclick()" type="success">等待读卡</el-button>-->
<!--        <el-button @click="endread_onclick()" type="warning">停止读卡</el-button>-->
        <el-button @click="handread_onclick()" type="success">读卡</el-button>
        <el-button type="danger" @click="myclose_onclick()">关闭机具</el-button>
<!--        <el-button type="warning" @click="MyClear_onclick()">清空信息</el-button>-->


      </el-row>
      </body>
      <!--      </div>-->
    </el-alert>
    <div class="form" style="margin-top: 10px">
      <!--      菜单内容-->
      <div style="width: 100%">
        <!--        表信息-->
        <div style="width:65%;float: left">
          <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                   label-width="100px">
            <el-col :span="20">
              <el-form-item label="部门" prop="departmentId">
                <template slot-scope="scope">
                  <el-select v-model="dataForm.hospitalId" filterable v-bind:disabled="dataForm.isHospitalManager==1"
                             placeholder="请选择医院" clearable @change="hospitalChanged">
                    <el-option
                      v-for="item in hospitalList"
                      :key="item.hospitalId"
                      :label="item.hospitalName"
                      :value="item.hospitalId">
                    </el-option>
                  </el-select>
                  <el-select v-model="dataForm.departmentId" filterable placeholder="请选择科室" clearable>
                    <el-option
                      v-for="item in departmentList"
                      :key="item.departmentId"
                      :label="item.departmentName"
                      :value="item.departmentId">
                    </el-option>
                  </el-select>
                </template>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-button type="primary" :disabled="!showFrom" @click="dataFormSubmit()">保存</el-button>
            </el-col>
            <el-col :span="8">
              <el-form-item label="姓名" prop="patientName">
                <el-input v-model="dataForm.patientName" placeholder="姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="身份证号" prop="idNumber">
                <el-input v-model="dataForm.idNumber" placeholder="身份证号码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="性别" prop="gender">
                <template slot-scope="scope">
                  <el-radio v-model="dataForm.gender" :label=1>男</el-radio>
                  <el-radio v-model="dataForm.gender" :label=2>女</el-radio>
                </template>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="住院时间" prop="hospitalizationDate">
                <el-date-picker
                  v-model="dataForm.hospitalizationDate"
                  type="date"
                  placeholder="选择住院时间"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="入院照片" prop="hospitalizationPicture">
                <img :src="dataForm.patientImg" width="40" height="40" class="head_pic"/>
              </el-form-item>
            </el-col>
            <!--            <el-col :span="12">-->
            <!--              <el-form-item label="病床号" prop="wardNumber">-->
            <!--                <el-input v-model="dataForm.wardNumber" placeholder="病床号"></el-input>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
            <!--            <el-col :span="12">-->
            <!--              <el-form-item label="病室号" prop="bedNumber">-->
            <!--                <el-input v-model="dataForm.bedNumber" placeholder="病室号"></el-input>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
            <!--            <el-col :span="24">-->
            <!--              <el-form-item label="其他信息" prop="otherMessage">-->
            <!--                <el-input v-model="dataForm.otherMessage" placeholder="其他信息"></el-input>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </div>
        <!--        身份证其他信息-->
        <!--        <div id="right" style="width:40%;float: left;margin-left: 3%">-->
        <!--          <h2>其他信息</h2>-->
        <!--          <el-table-->
        <!--            height="400"-->
        <!--            :data="tableData"-->
        <!--            style="width: 100%"-->
        <!--            :row-class-name="tableRowClassName">-->
        <!--            <el-table-column-->
        <!--              prop="label"-->
        <!--              label="标签"-->
        <!--              width="180">-->
        <!--            </el-table-column>-->
        <!--            <el-table-column-->
        <!--              prop="message"-->
        <!--              label="信息"-->
        <!--              width="180">-->
        <!--            </el-table-column>-->
        <!--          </el-table>-->
        <!--        </div>-->
      </div>
    </div>
  </div>
</template>

<script>
    export default {
        name: "readCard",
        data() {
            return {
                showFrom: true,
                message: '提示信息:',
                tableData: [],
                hospitalList: '',
                departmentList: '',
                dataForm: {
                    departmentPatientId: 0,
                    departmentId: '',
                    patientName: '',
                    idNumber: '',
                    gender: '',
                    wardNumber: '',
                    bedNumber: '',
                    hospitalizationDate: '',
                    leaveHospitalDate: '',
                    patientImg: '',
                    otherMessage: '',
                    remark: '',
                    hospitalId: this.$cookie.get('hospitalId') != "null" ? parseInt(this.$cookie.get('hospitalId')) : '',
                    isHospitalManager: this.$cookie.get('isHospitalManager')
                },
                dataRule: {
                    departmentId: [
                        {required: true, message: '部门id不能为空', trigger: 'blur'}
                    ],
                    patientName: [
                        {required: true, message: '姓名不能为空', trigger: 'blur'}
                    ],
                    idNumber: [
                        {required: true, message: '身份证号', trigger: 'blur'}
                    ],
                    gender: [
                        {required: true, message: '性别不能为空', trigger: 'blur'}
                    ],
                    hospitalizationDate: [
                        {required: true, message: '住院时间不能为空', trigger: 'blur'}
                    ],
                    patientImg: [
                        {required: true, message: '入院照片不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        mounted() {
            this.initActiveXObject();
            this.hospitalChanged(this.dataForm.hospitalId);
            //进入时关闭机具
            this.myclose_onclick();
        },
        created() {
            //加载医院
            this.$http({
                url: this.$http.adornUrl(`/bs/hospital/getList`),
                method: 'get',
                params:this.$http.adornParams()
            }).then(({data}) => {
                if (data && data.code === 0) {
                    this.hospitalList = data.res;
                }
            })
            // 初始化查询，默认为前一天
            this.dataForm.hospitalizationDate = this.getDate();
        },
        methods: {
            getUserInfo() {
                this.tableData = [];
                this.tableData.push({label: "设备模块号", message: rdcard.SecurityNum});
                this.tableData.push({label: "民族", message: rdcard.NationL});
                this.tableData.push({label: "出生日期", message: rdcard.BornL});
                this.tableData.push({label: "住址", message: rdcard.Address});
                this.tableData.push({label: "最新住址", message: rdcard.newAddress});
                this.tableData.push({label: "签发机关", message: rdcard.Police});
                this.tableData.push({label: "有效期限", message: rdcard.ActivityL});
                this.tableData.push({label: "指纹特征1", message: rdcard.FGchar1});
                var r1 = rdcard.FGNUM1;
                var r2 = rdcard.FGNUM2;
                var message1 = "";
                var message2 = "";
                switch (r1) {
                    case 11:
                        message1 = "右手拇指";
                        break;
                    case 12:
                        message1 = "右手食指";
                        break;
                    case 13:
                        message1 = "右手中指";
                        break;
                    case 14:
                        message1 = "右手环指";
                        break;
                    case 15:
                        message1 = "右手小指";
                        break;
                    case 16:
                        message1 = "左手拇指";
                        break;
                    case 17:
                        message1 = "左手食指";
                        break;
                    case 18:
                        message1 = "左手中指";
                        break;
                    case 19:
                        message1 = "左手环指";
                        break;
                    case 20:
                        message1 = "左手小指";
                        break;
                    default:
                        message1 = "";
                }
                switch (r2) {
                    case 11:
                        message2 = "右手拇指";
                        break;
                    case 12:
                        message2 = "右手食指";
                        break;
                    case 13:
                        message2 = "右手中指";
                        break;
                    case 14:
                        message2 = "右手环指";
                        break;
                    case 15:
                        message2 = "右手小指";
                        break;
                    case 16:
                        message2 = "左手拇指";
                        break;
                    case 17:
                        message2 = "左手食指";
                        break;
                    case 18:
                        message2 = "左手中指";
                        break;
                    case 19:
                        message2 = "左手环指";
                        break;
                    case 20:
                        message2 = "左手小指";
                        break;
                    default:
                        message2 = "";
                }
                this.tableData.push({label: "指位信息1", message: message1});
                r1 = rdcard.FGQualityScore1;
                r2 = rdcard.FGQualityScore2;
                var fgq1 = '';
                var fgq2 = '';
                if (r1 == 0)
                    fgq1 = "";
                else
                    fgq1 = r1;
                if (r2 == 0)
                    fgq2 = "";
                else
                    fgq2 = r2;
                this.tableData.push({label: "指纹质量1", message: fgq1});


                r1 = rdcard.FGRegistry1;
                r2 = rdcard.FGRegistry2;
                var fgr1 = '';
                var fgr2 = '';
                switch (r1) {
                    case 1:
                        fgr1 = "注册成功";
                        break;
                    case 2:
                        fgr1 = "注册失败";
                        break;
                    case 3:
                        fgr1 = "未注册";
                        break;
                    case 9:
                        fgr1 = "未知";
                        break;
                    default:
                        fgr1 = "";
                }
                switch (r2) {
                    case 1:
                        fgr2 = "注册成功";
                        break;
                    case 2:
                        fgr2 = "注册失败";
                        break;
                    case 3:
                        fgr2 = "未注册";
                        break;
                    case 9:
                        fgr2 = "未知";
                        break;
                    default:
                        fgr2 = "";
                }
                this.tableData.push({label: "指纹注册结果1", message: fgr1});
                //指纹特征2
                this.tableData.push({label: "指纹特征2", message: rdcard.FGchar2});
                this.tableData.push({label: "指位信息2", message: message2});
                this.tableData.push({label: "指纹质量2", message: fgq2});
                this.tableData.push({label: "指纹注册结果2", message: fgr2});

                //表单数据
                this.dataForm.patientImg = 'data:image/jpeg;base64,' + rdcard.JPGBuffer;
                this.dataForm.patientName = rdcard.NameS;
                this.dataForm.idNumber = rdcard.CardNo;
                var gender = rdcard.SexL;
                if (gender.indexOf("男") > -1) {
                    this.dataForm.gender = 1;
                } else {
                    this.dataForm.gender = 2;
                }

            },
            SaveCard_onclick() {
                try {
                    var pp
                    pp = rdcard.ExportBMP("C:/temp");
                    if (pp == 0) {
                        this.message = "保存成功";
                    } else {
                        this.message = "保存失败: " + pp;
                    }
                } catch (e) {
                    this.message = e.message
                }
            },

            myopen_onclick() {
                var pp;
                pp = rdcard.openport();
                //alert(pp);
                if (pp == 0) {
                    this.message = "提示信息: openport成功";
                    // return "openport成功";
                } else {
                    this.message = "提示信息: openport失败: " + pp;
                    // return "openport失败: " + pp;
                }
            },

            myclose_onclick() {
                try {
                    var pp
                    pp = rdcard.closeport();
                    if (pp == 0) {
                        this.message = "提示信息: closeport成功";
                    } else {
                        this.message = "提示信息: closeport失败: " + pp;
                        // return "closeport失败: " + pp;
                    }
                } catch (e) {
                    console.log(e)
                    this.message = "提示信息： 请检查机具是否连接";
                }
            },

            unload() {
                console.log("调用==unload")
                this.myclose_onclick();
            },

            endread_onclick() {
                var pp
                pp = rdcard.endread();
                if (pp == 0) {
                    this.message = "提示信息: endread成功";
                } else {
                    this.message = "提示信息: endread失败: " + pp;
                }
            },

            handread_onclick() {

                var pp
                pp = rdcard.readcard();
                if (pp == 0) {
                    this.message = "提示信息: readcard成功";
                } else {
                    this.message = "提示信息: readcard失败: " + pp;
                }
            },

            beginread_onclick() {
                var pp
                pp = rdcard.ReadCard2();
                if (pp == 0) {
                    this.message = "提示信息: ReadCard2成功";
                } else {
                    this.message = "提示信息: ReadCard2失败: " + pp;
                }
            },

            read3_onclick() {
                var pp
                pp = rdcard.ReadCard3();
                if (pp == 0) {
                    this.message = "提示信息: 等待读卡成功";
                } else {
                    this.message = "提示信息: 等待读卡失败: " + pp;
                }
            },

            window_onUnload() {
                try {
                    rdcard.DeleteOutputFile();
                    rdcard.DeleteAllPicture();
                } catch (e) {
                    console.log(e)
                }
            },
            //清空信息
            MyClear_onclick() {
                var pp
                pp = rdcard.ClearAll();
                this.message = "";
                if (pp == 0) {
                    this
                    this.message = "提示信息: 清空成功";
                    this.window_onUnload();
                    this.tableData = [];
                    //表单数据
                    this.dataForm.patientImg = "";
                    this.dataForm.patientName = "";
                    this.dataForm.idNumber = "";
                    this.dataForm.gender = "";
                } else {
                    this.message = "提示信息: 清空失败: " + pp;
                }
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            }
            ,
            initActiveXObject() {
                var scriptStr = document.createElement('script')
                scriptStr.setAttribute('for', 'idcard')
                scriptStr.event = 'Readed()'
                scriptStr.appendChild(document.createTextNode('activeXListener.getUserInfo()'))
                document.body.appendChild(scriptStr)
                //Closed() Opened()
                var scriptStr1 = document.createElement('script')
                scriptStr1.setAttribute('for', 'idcard')
                scriptStr1.event = 'Closed()'
                scriptStr1.appendChild(document.createTextNode('activeXListener.callBack("Closed")'))
                document.body.appendChild(scriptStr1)

                var scriptStr2 = document.createElement('script')
                scriptStr2.setAttribute('for', 'idcard')
                scriptStr2.event = 'Opened()'
                scriptStr2.appendChild(document.createTextNode('activeXListener.callBack("Opened")'))
                document.body.appendChild(scriptStr2)
                window.activeXListener = this
            }
            ,
            callBack(msg) {
                console.log("chenggguolll============" + msg)
            }
            ,
            hospitalChanged(hospitalId) {
                console.log(hospitalId)
                if (hospitalId != '') {
                    //加载科室
                    this.$http({
                        url: this.$http.adornUrl(`/bs/department/getListByHospitalId`),
                        method: 'get',
                        params: this.$http.adornParams({
                            hospitalId: hospitalId
                        })
                    }).then(({data}) => {
                        if (data && data.code === 0) {
                            this.dataForm.departmentId = '';
                            this.departmentList = data.res;
                        }
                    })
                } else {
                    this.departmentList = [];
                    this.dataForm.departmentId = '';
                }
            }
            ,
            getDate() {
                var myDate = new Date(); //获取系统当前时间
                return this.dateFtt("yyyy-MM-dd", myDate);
            }
            ,
            /**************************************时间格式化处理************************************/
            dateFtt(fmt, date) { //author: meizz
                var o = {
                    "M+": date.getMonth() + 1, //月份
                    "d+": date.getDate(), //日
                    "h+": date.getHours(), //小时
                    "m+": date.getMinutes(), //分
                    "s+": date.getSeconds(), //秒
                    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                    "S": date.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt))
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt))
                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            }
            ,
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/bs/departmentPatient/save`),
                            method: 'post',
                            data: this.$http.adornData({
                                'departmentId': this.dataForm.departmentId,
                                'hospitalizationDate': this.dataForm.hospitalizationDate,
                                'otherMessage': this.dataForm.otherMessage,
                                'remark': this.dataForm.remark,
                                'patientName': this.dataForm.patientName,
                                'idNumber': this.dataForm.idNumber,
                                'gender': this.dataForm.gender,
                                'patientImg': this.dataForm.patientImg,
                                'wardNumber': this.dataForm.wardNumber,
                                'bedNumber': this.dataForm.bedNumber,
                            })
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        // this.visible = false
                                        // this.$emit('refreshDataList')
                                        this.MyClear_onclick()
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }

</script>

<style scoped>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
  .el-alert__title{
    color: red!important;
  }
</style>
